package com.lijing.ArrayStack;

import java.util.Stack;

public class ArrayStackDemo {
    public static void main(String[] args) {
        ArrayStack arrayStack = new ArrayStack(3);
        arrayStack.push(1);
        arrayStack.push(2);
        arrayStack.push(3);
        arrayStack.push(4);
        arrayStack.pop();
        arrayStack.pop();
        arrayStack.pop();
        arrayStack.pop();
        arrayStack.list();


    }
}

class ArrayStack{
    private int maxSize;
    private int[] stack;//数组模拟栈，数据放在该数组中
    private int top = -1;//表示栈顶，初始化为-1

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }

    /**
     * 判断栈满
     * @return true栈满
     */
    public boolean isFull(){
        return top == maxSize-1;
    }

    /**
     * 判断栈空
     * @return true栈空
     */
    public boolean isEmpty(){
        return top == -1;
    }
    //入栈
    public void push(int value){
        if(isFull()){
            System.out.println("stack is full!");
            return;
        }
        top ++;
        stack[top] = value;
    }
    //出栈
    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("stack is empty!");
        }
        int temp =  stack[top];
        top --;
        return temp;

    }
    //遍历栈
    public void list(){
        if(isEmpty()){
            System.out.println("stack is empty!");
            return;
        }
        for (int i = top; i >= 0 ; i--) {
            System.out.printf("stack[%d]=%d\n",i,stack[i]);
        }
    }
}